package com.app.newiptv.utils;

import android.util.Log;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class SystemTimeDate {
    private static String mYear; // 当前年
    private static String mMonth; // 月
    private static String mDay;
    private static String mWay;

    public static void getSystemTime(TextView timeTextView) {
        Date dateSystem = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH:mm");
        String str = dateFormat.format(dateSystem);
        String[] times = str.split("/");
        //String date = str.substring(2, 10);
        //dateTextView.setText(date);
        //String time = str.substring(11, str.length());
        timeTextView.setText(times[0] + "年" + times[1] + "月" + times[2] + "    " + times[3]);
    }

    public static void getAllSystemTime(TextView dateTextView,
                                        TextView timeTextView) {
        Date dateSystem = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH:mm");
        String str = dateFormat.format(dateSystem);
        String date = str.substring(0, 10);
        dateTextView.setText(date);
        String time = str.substring(11, str.length());
        timeTextView.setText(time);
    }

    /**
     * 获取当前日期几月几号
     */
    public static String getDateString() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
        }
        return mMonth + "月" + mDay + "日";
    }


    /**
     * 获取当前年月日
     *
     * @return
     */
    public static String StringData() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
        }
        return mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
    }


    /**
     * 根据当前日期获得是星期几
     *
     * @return
     */
    public static String getWeek(String time) {
        String Week = "";


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "星期日";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "星期一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "星期二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "星期三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "星期四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "星期五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "星期六";
        }
        return Week;
    }


    /**
     * 获取今天往后一周的日期（几月几号）
     */
    public static List<String> getSevendate() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        for (int i = 0; i < 7; i++) {
            mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) - i);// 获取当前日份的日期号码

            int lastMath = Integer.parseInt(mMonth) - 1;
            if (lastMath <= 0) {
                lastMath = 12;
            }
            //> MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),lastMath)
//            if(Integer.parseInt(mDay) <=0){
//                int maxDay=MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),lastMath);
//                mMonth=String.valueOf(Integer.parseInt(mMonth)-1);
//                if(Integer.parseInt(mMonth)<=0){
//                    mMonth=String.valueOf(12);
//                }
//                int day=Integer.parseInt(mDay);
//                int i1 = day - maxDay;
//                mDay=String.valueOf(maxDay+Integer.parseInt(mDay));
//            }
            if (Integer.parseInt(mDay) <= 0) {
                if (Integer.parseInt(mMonth) == 1) {
                    mYear = (Integer.parseInt(mYear) - 1) + "";
                }
                int maxDay = MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), lastMath);
                mMonth = String.valueOf(Integer.parseInt(mMonth) - 1);
                if (Integer.parseInt(mMonth) <= 0) {
                    mMonth = String.valueOf(12);
                }
                if (mMonth.length() < 2) {
                    mMonth = 0 + mMonth;
                }
                int day = Integer.parseInt(mDay);
                int i1 = day - maxDay;
                mDay = String.valueOf(maxDay + Integer.parseInt(mDay));
                if (mDay.length() < 2) {
                    mDay = 0 + mDay;
                }
            }
            String date = mMonth + "月" + mDay + "日";
            dates.add(date);
        }
        return dates;
    }


    /**
     * 获取今天往后一周的集合
     */
    public static List<String> getSevenweek() {
        String week = "";
        List<String> weeksList = new ArrayList<String>();
        List<String> dateList = getLastdate();
        for (String s : dateList) {
            if (s.equals(StringData())) {
                week = "今天";
            } else {
                week = getWeek(s);
            }
            weeksList.add(week);
        }
        return weeksList;
    }

    /**
     * 获取今天往后一周的日期（几月几号）
     */
    public static List<String> getLastdate() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        for (int i = 0; i < 7; i++) {
            mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) - i);// 获取当前日份的日期号码
            int lastMath = Integer.parseInt(mMonth) - 1;
            if (lastMath <= 0) {
                lastMath = 12;
            }
            //> MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear),lastMath)
            if (Integer.parseInt(mDay) <= 0) {
                if (Integer.parseInt(mMonth) == 1) {
                    mYear = (Integer.parseInt(mYear) - 1) + "";
                }
                int maxDay = MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), lastMath);
                mMonth = String.valueOf(Integer.parseInt(mMonth) - 1);
                if (Integer.parseInt(mMonth) <= 0) {
                    mMonth = String.valueOf(12);
                }
                if (mMonth.length() < 2) {
                    mMonth = 0 + mMonth;
                }
                int day = Integer.parseInt(mDay);
                int i1 = day - maxDay;
                mDay = String.valueOf(maxDay + Integer.parseInt(mDay));
                if (mDay.length() < 2) {
                    mDay = 0 + mDay;
                }
            }
            String date = mYear + "-" + mMonth + "-" + mDay;
            dates.add(date);
        }
        return dates;
    }

    /**
     * 得到当年当月的最大日期
     **/
    public static int MaxDayFromDay_OF_MONTH(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);//注意,Calendar对象默认一月为0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }

    public static String formatTime(int time) {
        int hour = time / (60 * 60 * 1000);
        int min = (time / (60 * 1000)) % 60;
        int second = (time / 1000) % 60;
        StringBuilder sb = new StringBuilder();
        if (hour < 10 && hour >= 0) {
            sb.append("0");
        }
        sb.append(hour).append(":");
        if (min < 10 && min >= 0) {
            sb.append("0");
        }
        sb.append(min).append(":");
        if (second < 10) {
            sb.append("0");
        }
        sb.append(second);
        return sb.toString();
    }

    /*
     * 将时间戳转换为时间HHmm
     */
    public static String stampToHHmmDate(String s) {
        if (s != null) {
            String res;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            long lt = new Long(s);
            Date date = new Date(lt * 1000);
            res = simpleDateFormat.format(date);
            return res;
        }
        return "";
    }

    /*
 * 将时间戳转换为时间MM月dd日
 */
    public static String stampToMMddDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
* 将时间戳转换为时间MM月dd日
*/
    public static String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }


    /*
* 将时间戳转换为时间MM月dd日
*/
    public static String stampToSystemDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
* 将时间戳转换为时间MM月dd日
*/
    public static String stampToTimeDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 时间对比
     *
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compare_date(long DATE1, long DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            String data1 = stampToTimeDate(DATE1);
            String data2 = stampToTimeDate(DATE2);
            Date dt1 = df.parse(data1);
            Date dt2 = df.parse(data2);
            if (dt1.getTime() > dt2.getTime()) {
                Log.d("tag", "---111111-------" + dt1.getTime() + "-----------" + dt2.getTime() + ">>>1");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                Log.d("tag", "---111111-------" + dt1.getTime() + "-----------" + dt2.getTime() + ">>>-1");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}
